/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Utility;

import CA.Ca;
import Utility.exceptions.ExceptionCertificatoNonCreato;
import java.io.ByteArrayInputStream;
import java.security.cert.CertificateEncodingException;
import java.security.cert.X509Certificate;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import org.bouncycastle.util.encoders.Base64;

/**
 *
 * @author diego
 */
public class Utility {
    
    public static int CRITTOGRAFIA = 2;
    public static int FIRMA = 1;
    
    public static int NUOVO_CERTIFICATO = 0;
    public static int RINNOVO_CERTIFICATO = 1;    

    // Url server
    public static String URL_SERVER = "http://127.0.0.1:8080/xmlrpc"; 
    
    // Url server
    public static String NOME_FILE_CHIAVE_FIRMA = "KeyFirma";     
    public static String NOME_FILE_CHIAVE_CRITTOGRAFIA = "KeyCrittografia";     
    public static String NOME_FILE_PKCS10_FIRMA = "pkcs10-Firma";     
    public static String NOME_FILE_PKCS10_CRITTOGRAFIA = "pkcs10-Crittografia";     
    
    
    // Politiche CRL
    // La Crl viene rigenerata solo quando un cliente effettua la e CRL è scaduta
    public static int POLITICA_CRL_SCADENZA_FISSA = 0;
    // La Crl viene rigenerata alla revoca di ogni certificato e la data di scadenza viene prolungata
    public static int POLITICA_CRL_RIGENERAZIONE_OGNI_RICHIESTA = 1;
    
   /**
    * Creo un certificato X509Certificate da Stringa
    * @param certificato
    * @return 
    */
    public static X509Certificate creaCertificatoDaStringa(String certificato) throws ExceptionCertificatoNonCreato{
        // Da stringa lo converto in byte e da byte in javax..X509Certificate ... e poi da javax a java.X509Certificate...
        byte[] val_byte = Base64.decode(certificato);
        javax.security.cert.X509Certificate x509 = null;
        try {
            x509 = javax.security.cert.X509Certificate.getInstance(val_byte);
            X509Certificate certificatoX509 = Utility.convert(x509);
            return certificatoX509;
        } catch (Exception ex) {
            throw new ExceptionCertificatoNonCreato();
        }       
    }
    
    /**
     * Converti da X509Certificate a String
     * @param cert
     * @return
     * @throws CertificateEncodingException 
     */
    public static String creaStringaDaCertificato(X509Certificate cert) throws CertificateEncodingException{
        byte[] certCoded = Base64.encode(cert.getEncoded());
        return new String(certCoded);
    }
    
    /**
     * Converto una data di tipo Date in tipo String
     * @param data
     * @return 
     */
    public static String convertDateToString(Date data)    {
 
       // Create an instance of SimpleDateFormat used for formatting 
       // the string representation of date (month/day/year)
       DateFormat df = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");

       // Get the date today using Calendar object.
       
       Date today = data; 
       //Date today = Calendar.getInstance().getTime();        
       
       // Using DateFormat format method we can create a string 
       // representation of a date with the defined format.
       String reportDate = df.format(today);

       // Print what date is today!
       //System.out.println("Report Date: " + reportDate);
       
       return reportDate;
 
    }   
    
/*
     * Funzione che converte un javax.security in java.security
     * in java.security
     */
    public static java.security.cert.X509Certificate convert(javax.security.cert.X509Certificate cert) {
        try {
            //System.out.println("..conversione a java da javax...");
            byte[] encoded = cert.getEncoded();
            ByteArrayInputStream bis = new ByteArrayInputStream(encoded);
            java.security.cert.CertificateFactory cf
                = java.security.cert.CertificateFactory.getInstance("X.509");
            //System.out.println("..conversione finita");
            return (java.security.cert.X509Certificate)cf.generateCertificate(bis);
        } catch (java.security.cert.CertificateEncodingException e) {
        } catch (javax.security.cert.CertificateEncodingException e) {
        } catch (java.security.cert.CertificateException e) {
        }
        return null;
    }    
}
